package com.dier.webservices.restful.controller;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.dier.webservices.restful.model.Film;
import com.dier.webservices.restful.model.FilmList;
import com.dier.webservices.restful.service.ISFilmService;


@Controller
@RequestMapping(value="/webservices/rest")
public class ControllerWebServicesRest{

	private static final Logger LOG = Logger.getLogger(ControllerWebServicesRest.class.getName());
	
	@Autowired
	private ISFilmService filmService;
	
	@RequestMapping(value = "/film/{name}", method=RequestMethod.GET)
    public String watchFilm(@PathVariable("name") String name, Model model, 
    		HttpServletRequest request, HttpServletResponse response){
		
		Film miPelicula = filmService.searchFilm(name);
        model.addAttribute("miPelicula", miPelicula);
        return "result";
	}
	
	@RequestMapping(value = "/film/all", method=RequestMethod.GET)
    public String watchAllFilm(Model model, HttpServletRequest request, HttpServletResponse response){
		
		FilmList peliculas = filmService.searchAllFilm();
        model.addAttribute("miPelicula", peliculas);
        return "resultList";
	}
	
	@RequestMapping(value = "/film/save", method=RequestMethod.PUT)
    public String saveFilm(@RequestBody Film film, Model model, HttpServletRequest request, HttpServletResponse response){
        
		Boolean isCorrect = filmService.saveOrUpdateFilm(film);
		model.addAttribute("isCorrect", "Film saved?: " + isCorrect);
        return "result";
	}
	
	@RequestMapping(value = "/film/update/{idFilm}", method=RequestMethod.POST)
    public String updateFilm(@PathVariable("idFilm") Integer filmId,
    						 @RequestParam(required = false, value = "nombre")   String filmNombre,
    						 @RequestParam(required = false, value = "duracion") String filmDuracion,
    						 @RequestParam(required = false, value = "sipnosis") String filmSipnosis,
    						 Model model, HttpServletRequest request, HttpServletResponse response){
		
        Film film = filmService.searchFilm(filmId);
        
        if (StringUtils.hasText(filmNombre))
			film.setNombre(filmNombre);
        if (StringUtils.hasText(filmDuracion))
			film.setDuracion(filmDuracion);
        if (StringUtils.hasText(filmSipnosis))
			film.setSipnosis(filmSipnosis);
		
		Boolean isCorrect = filmService.saveOrUpdateFilm(film);
		model.addAttribute("isCorrect", "Film updated?: " + isCorrect);
        return "result";
	}
	
	@RequestMapping(value = "/film/delete/{id}", method=RequestMethod.DELETE)
    public String deleteFilm(@PathVariable("id") Integer id, Model model, 
    		HttpServletRequest request, HttpServletResponse response){
        
		Boolean isCorrect = filmService.deleteFilm(id);
		model.addAttribute("isCorrect", "Film deleted?: " + isCorrect);
        return "result";
	}
	
	
	
	public ISFilmService getFilmService() {
		return filmService;
	}

	public void setFilmService(ISFilmService filmService) {
		this.filmService = filmService;
	}
}
